import timeit
from Numbers import gen_relatively_primes
from Primes import gen_nprimes
import collections
from fractions import gcd
from Primes import gen_primes, prime_factors
import operator

NN = 120000

allprimes = sorted(list(gen_nprimes(None, NN)))
PRIME_FACTORS = {1:[1]}
RADICALS = {1:1}
for i in range(2,NN):
    PRIME_FACTORS[i] = list(prime_factors(i,allprimes))
    RADICALS[i] = reduce(operator.mul, PRIME_FACTORS[i])

def radical3(a,b,c):
    pf = set(PRIME_FACTORS[a])
    pf.update(PRIME_FACTORS[b])
    pf.update(PRIME_FACTORS[c])
    return reduce(operator.mul, pf)


def predicate(a,b,c):
    return gcd(a, b) == 1 and gcd(a, c) == 1 and gcd(b, c) == 1 and radical3(a,b,c) < c

def test(n):
    for i in filter(lambda x: RADICALS[x] < x, range(3,n)):
        for j in range(1,i/2):
            if predicate(j, i-j, i):
                 yield (j,i-j,i)

def solve(n, f):
    cnt = 0
    s = 0
    for i in f(n):
        cnt += 1
        s += i[2]
        a,b,c = i
        print i
    print cnt,s

print(timeit.timeit("solve(120000, test)", setup="from __main__ import solve,test", number=1))
